﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.SSO.Core.Entities.Statistics;
using System.Data;
using ESF.General;

#endregion


namespace ESF.SSO.Core.DataAccess.Statistics
{
	public static class AdoOperationExecution
	{
		#region Read

		public static List<OperationExecution> BrowseTable (OperationExecution pSample)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_BrowseOperationExecution");

			AdoESFSSO.R.AddParameter(cmd, "pCustomOperationName", pSample.CustomOperationName, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddParameter(cmd, "pUserAlias", pSample.User.Alias, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddInitialDateParameter (cmd, "pExecutionDateTimeStart", pSample.filExecutionDateTimeStart);
			AdoESFSSO.R.AddFinalDateParameter(cmd, "pExecutionDateTimeEnd", pSample.filExecutionDateTimeEnd);

			return AdoESFSSO.R.GetList <OperationExecution> (cmd, ConvertTo);
		}


		public static OperationExecution ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			return new OperationExecution ()
			{
				CustomOperationName = DataHelper.GetString (pReader, pPrefix + "CustomOperationName"),
				XMLParams = DataHelper.GetString(pReader, pPrefix + "XMLParams"),
				User = AdoUser.ConvertTo (pReader, pPrefix),
				ExecutionDateTime = DataHelper.GetNullableDateTime (pReader, pPrefix + "ExecutionDateTime"),
				UserHostAddress = DataHelper.GetString(pReader, pPrefix + "UserHostAddress"),
				calYear = DataHelper.GetNullableInt (pReader, pPrefix + "calYear"),
				calMonth = DataHelper.GetNullableInt (pReader, pPrefix + "calMonth"),
				calOperationCount = DataHelper.GetNullableInt (pReader, pPrefix + "calOperationCount")
			};
		}


		public static int GetExecutionOperationCount (string pCustomOperationName = null)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("[esf_sso].P_GetExecutionOperationCount");
			
			AdoESFSSO.R.AddParameter(cmd, "pCustomOperationName", pCustomOperationName);

			return AdoESFSSO.R.GetScalar<int> (cmd);
		}

		#endregion


		#region Write

		public static void Register (OperationExecution pItem)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_RegisterOperationExecution");

			AdoESFSSO.W.AddParameter (cmd, "pCustomOperationName", pItem.CustomOperationName);
			AdoESFSSO.W.AddParameter (cmd, "pXMLParams", pItem.XMLParams);
			AdoESFSSO.W.AddParameter (cmd, "pUserAlias", pItem.User.Alias);
			AdoESFSSO.W.AddParameter (cmd, "pExecutionDateTime", pItem.ExecutionDateTime);
			AdoESFSSO.W.AddParameter (cmd, "pUserHostAddress", pItem.UserHostAddress);

			AdoESFSSO.W.Execute (cmd);
		}

		#endregion
	}
}
